<template>
  <div class="param-config-manage">
    <el-card shadow="hover">
      <div class="param-header">
        <el-input
          v-model="searchText"
          placeholder="搜索参数名称"
          clearable
          style="width:200px; margin-right:16px;"
        />
        <el-button type="primary" @click="handleAdd">新增参数</el-button>
      </div>
      <el-table :data="filteredList" border style="margin-top:16px;">
        <el-table-column prop="name" label="参数名称" width="160px"/>
        <el-table-column prop="weight" label="权重(%)" width="100px"/>
        <el-table-column prop="threshold" label="达标线" width="120px"/>
        <el-table-column prop="unit" label="单位" width="100px"/>
        <el-table-column prop="remark" label="备注" width="220px"/>
        <el-table-column label="操作" width="150px" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" text @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" text @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="400px" destroy-on-close>
      <el-form :model="editRow" :rules="formRules" ref="formRef" label-width="90px">
        <el-form-item label="参数名称" prop="name">
          <el-input v-model="editRow.name"/>
        </el-form-item>
        <el-form-item label="权重(%)" prop="weight">
          <el-input-number v-model="editRow.weight" :min="1" :max="100" style="width:100%;" />
        </el-form-item>
        <el-form-item label="达标线" prop="threshold">
          <el-input v-model="editRow.threshold"/>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="editRow.unit"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editRow.remark" type="textarea"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>
    <!-- 删除弹窗 -->
    <el-dialog v-model="deleteVisible" title="提示" width="320px" center>
      <div>确定要删除该参数项吗？</div>
      <template #footer>
        <el-button @click="deleteVisible=false">取消</el-button>
        <el-button type="danger" @click="confirmDelete">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

interface ParamItem {
  name: string
  weight: number
  threshold: string
  unit: string
  remark: string
}

// 初始参数模拟数据
const paramList = ref<ParamItem[]>([
  { name: '单位能耗', weight: 25, threshold: '<=40', unit: 'kWh/㎡·a', remark: '节能标准值' },
  { name: '碳排放', weight: 20, threshold: '<=18', unit: 'kgCO₂/㎡·a', remark: '绿色三星' },
  { name: '用水效率', weight: 15, threshold: '>=1.2', unit: 'm³/㎡·a', remark: '越高越好' },
  { name: '可再生能源利用', weight: 10, threshold: '>=10%', unit: '%', remark: '太阳能、地源热泵等' },
  { name: '室内环境', weight: 20, threshold: 'PM2.5<=35', unit: 'μg/m³', remark: '优良' },
  { name: '智能化水平', weight: 10, threshold: '>=3', unit: '星级', remark: '智能控制系统数量' }
])

const searchText = ref('')
const filteredList = computed(() => {
  let arr = paramList.value
  if (searchText.value) {
    arr = arr.filter(r => r.name.includes(searchText.value))
  }
  return arr
})

// 弹窗/表单逻辑
const dialogVisible = ref(false)
const dialogTitle = ref('新增参数')
const editRow = reactive<ParamItem>({
  name: '',
  weight: 1,
  threshold: '',
  unit: '',
  remark: ''
})
const formRef = ref()
const formRules = {
  name: [{ required: true, message: '必填', trigger: 'blur' }],
  weight: [{ required: true, message: '必填', trigger: 'blur' }],
  threshold: [{ required: true, message: '必填', trigger: 'blur' }],
  unit: [{ required: true, message: '必填', trigger: 'blur' }]
}

function handleAdd() {
  dialogTitle.value = '新增参数'
  Object.assign(editRow, { name: '', weight: 1, threshold: '', unit: '', remark: '' })
  dialogVisible.value = true
}
function handleEdit(row: ParamItem) {
  dialogTitle.value = '编辑参数'
  Object.assign(editRow, row)
  dialogVisible.value = true
}
function submitEdit() {
  formRef.value?.validate((valid: boolean) => {
    if (!valid) return
    if (dialogTitle.value.startsWith('编辑')) {
      const idx = paramList.value.findIndex(t => t.name === editRow.name)
      if (idx !== -1) paramList.value[idx] = { ...editRow }
      ElMessage.success('编辑成功')
    } else {
      paramList.value.unshift({ ...editRow })
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
  })
}
// 删除
const deleteVisible = ref(false)
const deleteTarget = ref<ParamItem | null>(null)
function handleDelete(row: ParamItem) {
  deleteTarget.value = row
  deleteVisible.value = true
}
function confirmDelete() {
  if (deleteTarget.value) {
    paramList.value = paramList.value.filter(t => t.name !== deleteTarget.value!.name)
    ElMessage.success('删除成功')
  }
  deleteVisible.value = false
}
</script>

<style scoped>
.param-config-manage {
  width: 100%;
  margin: 0;
  padding: 32px 0;
}
.param-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
</style>
